package ro.gillardon.beans;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

import ro.gillardon.commons.logger.BTPLogger;

public final class BeanProvider {
  private static final Logger LOG = Logger.getLogger(BeanProvider.class);

  /** Prohibit Instantiation. */
  private BeanProvider() {
    // private constructor
  }

  /**
   * Looks up the corresponging ejb.
   *
   * @param ejbName the name of ejb
   * @return the ejb
   */
  public static Object getEJB(final String ejbName) {
    String name = "java:global/businesstripplanner-ear/businesstripplanner-business-0.0.1-SNAPSHOT/" + ejbName;
    try {
      // Create the initial context
      Context ctx = new InitialContext();

      // Look up an object
      Object obj = ctx.lookup(name);

      // Print it
      BTPLogger.info(LOG, name + " is bound to: " + obj);
      return obj;

    } catch (NamingException e) {
      BTPLogger.error(LOG, "Problem looking up " + name + ": " + e);
    }
    return null;
  }
}
